const BaseHandler = require('../base-handler');
const SystemServices = require('../../../constants').SystemServices;
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;

class AssociatorHandler extends BaseHandler {
    get pattern() {
        return "/users/:type";
    }

    doGET(request, response) {
        if (request.session.user.authenticated) {
            const mapList = ['/users/associator', '/taskmanage', '/dashboard', '/workflowsManage', '/settings']
            this.Controller.commonSearch({ type: 'role', userId: request.session.user.id }, (result) => {
                if (result.error)
                    return response.render('settings', {
                        user: request.session.user
                    });
                request.session.user.roleIds = result.roleIds;
                var roleIds = result.roleIds && result.roleIds.length ? result.roleIds.split(',') : [];
                if (roleIds.length) {
                    if (roleIds.includes('1'))
                        return response.render('associator', {
                            user: request.session.user
                        });
                    response.redirect(mapList[roleIds[0] - 1]);
                } else {
                    response.redirect('/no/found');
                }
            })
        } else {
            response.redirect('/login');
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
    }
}

module.exports = AssociatorHandler;